<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../../js/vue.js"></script>
</head>
<style>
    * {
        margin: 0;
        padding: 0;
    }

    #app {
        width: 400px;
        margin: 0 auto;
    }

    .tr {
        height: 40px;
        line-height: 40px;
        background-color: #000;
        color: tomato;
    }

    .item {
        float: left;
        width: 100px;
        text-align: center;
    }
</style>

<body>
    <div id="app">
        <myadd @addlistbyitem="addListByItem" :list="list"></myadd>
        <mylist :list="list" @dellistbyid="delListById"></mylist>
    </div>
</body>
<template id="mylist">
    <div class="table">
        <div class="tr">
            <div class="item">编号</div>
            <div class="item">用户名</div>
            <div class="item">年龄</div>
            <div class="item">操作</div>
        </div>
        <div class="tr" v-for="item in list" :key="item.id">
            <div class="item">{{item.id}}</div>
            <div class="item">{{item.username}}</div>
            <div class="item">{{item.userage}}</div>
            <div class="item" @click="del(item.id)">删除</div>
        </div>
    </div>
</template>

<template id="myadd">
    <div class="add">
        <p><label>用户名：<input v-model="username" /></label></p>
        <p><label>密&emsp;码：<input v-model="userage" /></label></p>
        <button @click="add">增加</button>
    </div>
</template>
<script>
    let mylist = {
        props: ["list"],
        template: "#mylist",
        methods: {
            del(id) {//删除，点击删除，调用父元素的删除方法dellistbyid()
                this.$emit("dellistbyid", id);
            },
        }
    }

    let myadd = {
        props: ["list"],
        template: "#myadd",
        data() {
            return {
                username: "",
                userage: ""
            }
        },
        methods: {
            add() {
                // console.log(this.username, this.userage);
                if (this.list.some(item => item.username == this.username)) {
                    alert("该用户名已存在！");
                } else {
                    let id = 1;
                    if (this.list.length) {
                        id = this.list.map(item => item.id).sort((a, b) => b - a)[0] + 1;
                    }
                    this.$emit("addlistbyitem", { //点击增加的时候，调用父元素的增加方法
                        id,
                        username: this.username,
                        userage: this.userage
                    })
                }

            }
        }
    }



    new Vue({
        el: "#app",
        data: {
            list: [
                {id: 1, username: "lili", userage: 18},
                {id: 2, username: "lala", userage: 17},
                {id: 3, username: "lulu", userage: 20},
                {id: 4, username: "keke", userage: 21}
            ]
        },
        methods: {
            delListById(id) { //删除方法
                this.list = this.list.filter(item => item.id != id);
            },
            addListByItem(item) { //增加方法
                this.list.push(item);
            }
        },
        components: {
            mylist,
            myadd
        }
    })
</script>

</html>